package com.lanou.tk.tourism.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;

import com.lanou.tk.tourism.R;
import com.lanou.tk.tourism.adapter.MainActivityAdapter;
import com.lanou.tk.tourism.fragment.CommunityFragment;
import com.lanou.tk.tourism.fragment.RecommendFragment;
import com.lanou.tk.tourism.fragment.TargetFragment;
import com.lanou.tk.tourism.login.CellphoneLoginActivity;
import com.lanou.tk.tourism.login.PersonalActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity implements View.OnClickListener {

    private TabLayout tab_fragment_title;//定义tablayout
    private ViewPager viewPager;//定义viewPager
    private FragmentPagerAdapter fAdapter;//定义adapter

    private List<Fragment> list_fragment;//定义要装fragment的列表
    private List<String> list_title;//tab名称列表


    private Button individualBtn;  //个人
    private Button searchBtn;    //搜索


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);
        initView();
    }
   private void initView(){
       tab_fragment_title = (TabLayout) findViewById(R.id.tabLayout);
       viewPager = (ViewPager) findViewById(R.id.viewPager);
       tab_fragment_title.setTabMode(tab_fragment_title.MODE_SCROLLABLE);
       tab_fragment_title.setSelectedTabIndicatorHeight(10);

       //按钮
       individualBtn = (Button) findViewById(R.id.individualBtn);
       searchBtn = (Button) findViewById(R.id.searchBtn);
       individualBtn.setOnClickListener(this);
       searchBtn.setOnClickListener(this);






       //将fragment装进列表中
       list_fragment = new ArrayList<>();
       list_fragment.add(new RecommendFragment());
       list_fragment.add(new TargetFragment());
       list_fragment.add(new CommunityFragment());


       //将名字加载tab名字列表,正常情况下,我们应该values/arrays.xml中进行定义然后调用
       list_title = new ArrayList<>();
       list_title.add("推荐");
       list_title.add("目的地");
       list_title.add("社区");

       //设置tabLayout的模式
       tab_fragment_title.setTabMode(TabLayout.MODE_FIXED);



       fAdapter = new MainActivityAdapter(getSupportFragmentManager(),list_fragment,list_title);


       //viewpager加载adapter
       viewPager.setAdapter(fAdapter);
       //Tablayout加载viewpager
       tab_fragment_title.setupWithViewPager(viewPager);

   }
    //点击事件
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.individualBtn:
                Intent intent0 = new Intent(MainActivity.this, PersonalActivity.class);
                startActivity(intent0);
                break;
            case R.id.searchBtn:
                Intent intent1 = new Intent(MainActivity.this, CellphoneLoginActivity.class);
                startActivity(intent1);
                break;
        }



    }



}


